import type {
  AddReq,
  CreateCrudOptionsRet,
  DelReq,
  EditReq,
  UserPageQuery,
} from '@fast-crud/fast-crud';

import { dict } from '@fast-crud/fast-crud';

import * as api from './api';

export default function crud(): CreateCrudOptionsRet {
  return {
    crudOptions: {
      request: {
        addRequest: async ({ form }: AddReq) => await api.AddObj(form),
        delRequest: async ({ row }: DelReq) => await api.DelObj(row.id),
        editRequest: async ({ form }: EditReq) => await api.UpdateObj(form),
        pageRequest: async (query: UserPageQuery) => await api.GetPage(query),
      },
      toolbar: {},
      table: {},
      rowHandle: { fixed: 'right' },
      columns: {
        id: {
          title: 'ID',
          type: 'text',
          form: { show: false },
          column: { show: false },
        },
        name: {
          title: '连接名',
          type: ['text'],
          search: { show: true },
          column: { ellipsis: true, width: 200 },
          form: {
            wrapperCol: { span: 9 },
            col: { span: 24 },
            helper: '数据连接名称',
            rules: [{ min: 2, max: 50, message: '长度在 2 到 50 个字符' }],
          },
          editForm: { component: { disabled: true } },
        },
        username: {
          title: '用户名',
          type: 'text',
          column: { width: 120 },
          form: {
            rules: [
              { required: true, message: '请输入账号名' },
              { min: 4, max: 30, message: '长度在 4 到 30 个字符' },
            ],
          },
        },
        password: {
          title: '密码',
          type: 'password',
          column: { show: false, width: 120, ellipsis: true },
          form: {
            rules: [
              { required: true, message: '请输入密码' },
              { min: 4, max: 30, message: '长度在 4 到 30 个字符' },
            ],
          },
        },
        dbType: {
          title: 'DB类型',
          type: 'dict-select',
          search: { show: true },
          column: { ellipsis: true, width: 120 },
          addForm: { value: 'mysql' },
          dict: dict({
            data: [
              { value: 'mysql', label: 'MySQL', color: 'success' },
              { value: 'oracle', label: 'Oracle', color: 'success' },
              { value: 'db2', label: 'DBDB22', color: 'success' },
              { value: 'postgresql', label: 'PostgreSQL', color: 'success' },
              { value: 'sqlserver', label: 'SQL Server', color: 'success' },
            ],
          }),
          form: {
            helper: '暂时只支持MySQL数据库哦',
            rules: [{ required: true, message: '请选择数据库类型' }],
          },
        },
        driverClassName: {
          title: '连接驱动',
          type: ['dict-select'],
          column: { ellipsis: true, width: 200 },
          addForm: { value: 'com.mysql.cj.jdbc.Driver' },
          dict: dict({
            data: [
              {
                value: 'com.mysql.cj.jdbc.Driver',
                label: 'com.mysql.cj.jdbc.Driver',
                color: 'success',
              },
              {
                value: 'oracle.jdbc.driver.OracleDriver',
                label: 'oracle.jdbc.driver.OracleDriver',
                color: 'success',
              },
              {
                value: 'org.postgresql.Driver',
                label: 'org.postgresql.Driver',
                color: 'success',
              },
              {
                value: 'com.ibm.db2.jcc.DB2Driver',
                label: 'com.ibm.db2.jcc.DB2Driver',
                color: 'success',
              },
              {
                value: 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
                label: 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
                color: 'success',
              },
            ],
          }),
          form: {
            helper: '暂时只支持MySQL数据库哦',
            rules: [{ required: true, message: '请选择数据库类型' }],
          },
        },
        host: {
          title: 'Host',
          type: ['textarea'],
          column: { show: true, width: 120, ellipsis: true },
          form: {
            col: { span: 24 },
            helper: 'Host格式 ip:port ',
            rules: [
              { required: true, message: 'Host不能为空' },
              { min: 8, max: 100, message: '长度在 8 到 100 个字符' },
            ],
          },
        },
        locked: {
          title: '状态',
          type: ['dict-radio'],
          search: { show: true },
          addForm: { value: false },
          column: { show: true, align: 'center', width: 80 },
          dict: dict({
            data: [
              { value: false, label: '启用', color: 'success' },
              { value: true, label: '禁用', color: 'error' },
            ],
          }),
        },
        remark: {
          title: '描述信息',
          type: ['textarea'],
          column: { ellipsis: true, show: false },
          form: { col: { span: 24 } },
        },
        createdTime: {
          title: '创建时间',
          type: ['datetime', 'wp-readonly-time'],
        },
      },
    },
  };
}
